# 构建阶段
FROM node:16 AS builder

WORKDIR /app

# 复制package文件
COPY package*.json ./
COPY server/package*.json ./server/

# 安装所有依赖
RUN npm install
RUN cd server && npm install

# 复制源代码
COPY . .

# 构建前端
RUN npm run build

# 构建后端
RUN cd server && npm run build

# 生产阶段
FROM node:16-alpine

WORKDIR /app

# 复制package文件
COPY package*.json ./
COPY server/package*.json ./server/

# 只安装生产依赖
RUN npm install --only=production
RUN cd server && npm install --only=production

# 复制构建产物
COPY --from=builder /app/build ./build
COPY --from=builder /app/server/dist ./server/dist
COPY server/.env.example ./server/.env

# 暴露端口
EXPOSE 3000 3001

# 启动命令
CMD ["sh", "-c", "cd server && npm start & cd .. && npx serve -s build"]